package galaxy.math;

import static processing.core.PApplet.*;
import processing.core.PVector;

public class AffineTransform {
	Matrix m;

	// Create a transform with a given matrix
	AffineTransform(Matrix m) {
		this.m = m;
	}

	// Apply the transform to a vector
	public PVector apply(PVector p) {
		return this.m.multiply(p);
	}

	// Create a combination of two affine transformations
	public static AffineTransform combine(AffineTransform a, AffineTransform b) {
		return new AffineTransform(a.m.multiply(b.m));
	}

	// Constructs a rotation transform
	public static AffineTransform rotate(float angle) {
		return new AffineTransform(new Matrix(
			new float[] { cos(angle), sin(angle), 0 },
			new float[] { -sin(angle), cos(angle), 0 },
			new float[] { 0, 0, 1 }
		));
	}

	// Constructs a translation transform
	public static AffineTransform translate(PVector p) {
		return new AffineTransform(new Matrix(
			new float[] { 1, 0, p.x },
			new float[] { 0, 1, p.y },
			new float[] { 0, 0, 1 }
		));
	}
}


